<template>
  <div class="exam">
<!--    搜索栏-->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="formInline.key" placeholder="请输入关键字" clearable></el-input>
      </el-form-item>
      <el-form-item label="宣讲会类型">
        <el-select v-model="formInline.type" placeholder="宣讲会类型" clearable>
          <el-option label="空中宣讲会" value="网络"></el-option>
          <el-option label="线下宣讲会" value="现场"></el-option>
          <el-option label="全部宣讲会" value="all"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="getMeetingInfo">重置</el-button>
      </el-form-item>
    </el-form>
<!--    表格-->
    <el-table
      :data="tableData"
      style="width: 100%"
      border>
      <el-table-column
        prop="Name"
        label="单位名称">
      </el-table-column>

      <el-table-column
        prop="startDate"
        label="开始时间">

      </el-table-column>

      <el-table-column
        prop="endDate"
        label="结束时间">

      </el-table-column>

      <el-table-column
        prop="Address"
        label="举办地点">

      </el-table-column>

    </el-table>
    <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="pagination.current"
    :page-sizes="[5, 10]"
    :page-size="pagination.size"
    layout="total, sizes, prev, pager, next, jumper"
    :total="pagination.total"
    class="page"
  ></el-pagination>
  </div>

</template>

<script>
export default {
  data() {
    return {
      tableData:[],
      pagination: {
        //分页后的考试信息
        current: 1, //当前页
        total: null, //记录条数
        size: 5 //每页条数
      },
      formInline: {
        key: null,
        type: null
      }
    };
  },
  created() {
    this.getMeetingInfo();
  },
  methods: {
    getMeetingInfo() {
      this.$axios({
        url:'/api/meeting/talkList',
        method:'get',
        params:{
          page:this.pagination.current,
          size:this.pagination.size
        },
        headers:{
          'Authorization':this.$cookies.get("token")
        }
      }).then(res=>{
        this.pagination=res.data.data
        this.tableData = res.data.data.records
        for(let i=0;i<this.tableData.length;i++){
          this.tableData[i].startDate=this.formatDateTime(this.tableData[i].startDate)
          this.tableData[i].endDate=this.formatDateTime(this.tableData[i].endDate)
        }
        console.log(this.tableData)
      })
    },
    //改变当前记录条数
    handleSizeChange(val) {
      this.pagination.size = val;
      if((this.formInline.type==="all"||this.formInline.type==null)&&this.formInline.key===null)
       this.getMeetingInfo();
      else
        this.onSubmit();
    },
    //改变当前页码，重新发送请求
    handleCurrentChange(val) {
      this.pagination.current = val;
      if((this.formInline.type==="all"||this.formInline.type==null)&&this.formInline.key===null)
        this.getMeetingInfo();
      else
        this.onSubmit();
    },
    //日期格式化
    formatDateTime(dateString) {
      // 日期时间格式化逻辑
      const date = new Date(dateString);
      const year = date.getFullYear().toString() // 获取年份的后两位
      const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份用0填充到两位
      const day = date.getDate().toString().padStart(2, '0'); // 日期用0填充到两位
      const hours = date.getHours().toString().padStart(2, '0'); // 小时用0填充到两位
      const minutes = date.getMinutes().toString().padStart(2, '0'); // 分钟用0填充到两位

      return `${year}-${month}-${day} ${hours}:${minutes}`;
    },

    //查询
    onSubmit(){
      if(this.formInline.type!=null&&this.formInline.key==null){
        if(this.formInline.type === "all"){
          this.getMeetingInfo()
        }
        else {
          this.$axios({
            url:'/api/meeting/sort',
            method:'get',
            params:{
              page:this.pagination.current,
              size:this.pagination.size,
              type:this.formInline.type
            },
            headers:{
              "Authorization":this.$cookies.get("token")
            }
          }).then(res =>{
            this.pagination=res.data.data
            this.tableData = res.data.data.records
            for(let i=0;i<this.tableData.length;i++){
              this.tableData[i].startDate=this.formatDateTime(this.tableData[i].startDate)
              this.tableData[i].endDate=this.formatDateTime(this.tableData[i].endDate)
            }
          })
        }
      }
      if(this.formInline.type==null&&this.formInline.key!=null){
        this.$axios({
          url:'/api/meeting/findByKey',
          method:'get',
          params:{
            page:this.pagination.current,
            size:this.pagination.size,
            key:this.formInline.key
          },
          headers:{
            "Authorization":this.$cookies.get("token")
          }
        }).then(res =>{
          this.pagination=res.data.data
          this.tableData = res.data.data.records
          //时间格式修改
          for(let i=0;i<this.tableData.length;i++){
            this.tableData[i].startDate=this.formatDateTime(this.tableData[i].startDate)
            this.tableData[i].endDate=this.formatDateTime(this.tableData[i].endDate)
          }
        })
      }
      if(this.formInline.type!=null&&this.formInline.key!=null){
        if(this.formInline.type==="all"){
          this.$axios({
            url:'/api/meeting/findByKey',
            method:'get',
            params:{
              page:this.pagination.current,
              size:this.pagination.size,
              key:this.formInline.key
            },
            headers:{
              "Authorization":this.$cookies.get("token")
            }
          }).then(res =>{
            this.pagination=res.data.data
            this.tableData = res.data.data.records
            for(let i=0;i<this.tableData.length;i++){
              this.tableData[i].startDate=this.formatDateTime(this.tableData[i].startDate)
              this.tableData[i].endDate=this.formatDateTime(this.tableData[i].endDate)
            }
          })
        }
        else {
          this.$axios({
            url:'/api/meeting/MultiFind',
            method:'get',
            params:{
              page:this.pagination.current,
              size:this.pagination.size,
              key:this.formInline.key,
              type:this.formInline.type
            },
            headers:{
              "Authorization":this.$cookies.get("token")
            }
          }).then(res =>{
            this.pagination=res.data.data
            this.tableData = res.data.data.records
            for(let i=0;i<this.tableData.length;i++){
              this.tableData[i].startDate=this.formatDateTime(this.tableData[i].startDate)
              this.tableData[i].endDate=this.formatDateTime(this.tableData[i].endDate)
            }
          })
        }
      }
      if(this.formInline.type==null&&this.formInline.key==null){
        this.$message.warning("查找条件为空！")
      }
    }
  },
};
</script>
<style lang="less" scoped>
.exam {
  padding: 0px 40px;
  .page {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .edit {
    margin-left: 20px;
  }
  .el-table tr {
    background-color: #DD5862 !important;
  }
}
  .el-table .warning-row {
    background: #000 !important;

  }

  .el-table .success-row {
    background: #DD5862;
  }

</style>
